package com.liang.pcc.excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.liang.pcc.domain.BusParam;
import com.liang.pcc.excel.BaseExcelListener;
import com.liang.pcc.excel.model.BusParamInvestigation;
import com.liang.pcc.service.BusParamService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * 母线参数监听器
 */
@Slf4j
public class BusParamListener extends BaseExcelListener<BusParamInvestigation> {

    private final BusParamService busParamService;
    private final Long fileId;
    private final Long userId;

    private final List<BusParam> dataList = new ArrayList<>();

    public BusParamListener( BusParamService busParamService, CountDownLatch latch, Long fileId, Long userId) {
        super(latch);
        this.busParamService = busParamService;
        this.fileId = fileId;
        this.userId = userId;
    }

    @Override
    public void invoke(BusParamInvestigation rowData, AnalysisContext analysisContext) {
        BusParam busParam = BusParam.builder()
                .region(rowData.getRegion())
                .busName(rowData.getBusName())
                .voltageLevel(rowData.getVoltageLevel())
                .largeImpedance(rowData.getLageModeShortCircuitImpedance())
                .smallImpedance(rowData.getSmallModeShortCircuitImpedance())
                .resistance(rowData.getResistance())
                .shortCircuitCurrentLimit(rowData.getShortCircuitCurrentLimit())
                .voltageDeviation(rowData.getVoltageDeviation())
                .maxPositiveDeviation(rowData.getMaxPositiveVoltageDeviationAllowed())
                .maxNegativeDeviation(rowData.getMaxNegativeVoltageDeviationAllowed())
                .fileId(fileId)
                .userId(userId)
                .build();
        log.info("解析到数据: {}", busParam);
        dataList.add(busParam);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        busParamService.saveBatch(dataList);
        super.doAfterAllAnalysed(analysisContext);
        log.info("Sheet1数据插入完成！");
    }
}
